package com.hyl.controller;

import com.alibaba.fastjson.JSONObject;
import com.hyl.entity.SysRole;
import com.hyl.service.SysRoleService;
import com.hyl.vo.PageRequest;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Description TODO
 * @Author hyl
 * @Date 2023/5/15 9:47
 **/
@Controller
@RequestMapping("/role")
public class RoleController {

	private Logger logger = Logger.getLogger(RoleController.class);

	@Resource
	private SysRoleService roleService;

	@GetMapping("/list")
	public String list(Model  model){
		List<SysRole> list = roleService.findAll();
		//
		//数据传递
		model.addAttribute("roleList", list);
		return "role/list";//视图名称
	}

	@GetMapping("/page")
	public String page(Model  model, PageRequest info){
		List<SysRole> list = roleService.findByParams(
				(String) info.getParams().get("roleName"),
				(String) info.getParams().get("code"),
				info.getOffset(), info.getShowSize());
		//数据传递
		model.addAttribute("roleList", list);
		return "role/list";//视图名称
	}


	//删除
	@GetMapping("/delete/{id}") //rest风格==》{id}==>占位符==》将数据通过@PathVariable注解注入到参数中
	public String delete(@PathVariable(name = "id") Integer id){
		//删除
		roleService.del(id);
		return "redirect:list";//重定向
	}

	//新增
	@PostMapping("/add")
	public String add(SysRole role){
		roleService.save(role);
		return "redirect:list";//重定向
	}

	//编辑详情
	@GetMapping("/edit/{id}") //rest风格==》{id}==>占位符==》将数据通过@PathVariable注解注入到参数中
	public String edit(@PathVariable(name = "id") Integer id){
		//删除
		roleService.findById(id);
		return "user/update";//重定向
	}

	//编辑/修改
	@PostMapping("/modify")
	public String modify(SysRole role){
		roleService.update(role);
		return "redirect:list";//重定向
	}

	@ResponseBody //返回结果=》ajax
	@GetMapping(value = "/checkName/{name}",produces = "text/html;charset=UTF-8")
	public String checkName(@PathVariable String name){
		//验证数据是否正确
		return "张三";
	}

	@ResponseBody
	@GetMapping("/listjson")
	public String listjson(){
		List<SysRole> list = roleService.findAll();
		return JSONObject.toJSONString(list);//视图名称
	}


}
